import * as THREE from "three"
const A = new THREE.Vector3(),
  C = new THREE.Matrix4(),
  S = new THREE.Vector3(1, 0, 0),
  U = new THREE.Vector3(0, 1, 0),
  T = new THREE.Vector3(0, 0, 1),
  j = new THREE.Vector3(),
  L = new THREE.Vector3(),
  R = new THREE.Vector3(),
  y = new THREE.Vector3(0, 1, 0),
  k = new THREE.Quaternion(),
  r = new THREE.Vector3()

export default class TransformControlsPlane extends THREE.Mesh {
  space: any
  mode: string
  worldQuaternion: THREE.Quaternion
  cameraQuaternion: THREE.Quaternion
  worldPosition: THREE.Vector3
  axis: any
  isTransformControlsPlane: boolean
  eye: any
  constructor() {
    super(
      new THREE.PlaneGeometry(1e5, 1e5, 2, 2),
      new THREE.MeshBasicMaterial({
        visible: !1,
        wireframe: !0,
        side: THREE.DoubleSide,
        transparent: !0,
        opacity: 0.1,
        toneMapped: !1
      })
    )
    this.type = "TransformControlsPlane"
  }
  updateMatrixWorld(t) {
    let e = this.space
    this.position.copy(this.worldPosition)
    "scale" === this.mode && (e = "local")
    j.copy(S).applyQuaternion("local" === e ? this.worldQuaternion : k)
    L.copy(U).applyQuaternion("local" === e ? this.worldQuaternion : k)
    R.copy(T).applyQuaternion("local" === e ? this.worldQuaternion : k)
    y.copy(L)
    switch (this.mode) {
      case "translate":
      case "scale":
        switch (this.axis) {
          case "X":
            y.copy(this.eye).cross(j), A.copy(j).cross(y)
            break
          case "Y":
            y.copy(this.eye).cross(L), A.copy(L).cross(y)
            break
          case "Z":
            y.copy(this.eye).cross(R), A.copy(R).cross(y)
            break
          case "XY":
            A.copy(R)
            break
          case "YZ":
            A.copy(j)
            break
          case "XZ":
            y.copy(R), A.copy(L)
            break
          case "XYZ":
          case "E":
            A.set(0, 0, 0)
        }
        break
      case "rotate":
      default:
        A.set(0, 0, 0)
    }
    0 === A.length() ? this.quaternion.copy(this.cameraQuaternion) : (C.lookAt(r.set(0, 0, 0), A, y), this.quaternion.setFromRotationMatrix(C))
    super.updateMatrixWorld(t)
  }
}
TransformControlsPlane.prototype.isTransformControlsPlane = !0
